package com.kingschan.jeasytag.web.listen;

import java.io.File;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.apache.log4j.Logger;
import com.kingschan.jeasytag.container.impl.EasyTagContainerImpl;
import com.kingschan.util.zip.DeCompressUtil;
@WebListener
public class EasyTagLoadListen implements ServletContextListener{
	private static Logger log = Logger.getLogger(EasyTagLoadListen.class);
	
	@Override
	public void contextDestroyed(ServletContextEvent scontent) {
		
		
	}

	@Override
	public void contextInitialized(ServletContextEvent scontent) {		
		EasyTagContainerImpl container=EasyTagContainerImpl.getInstance();
		container.setSpath(scontent.getServletContext().getRealPath("").concat("\\WEB-INF\\").concat(container.getSpath()).concat("\\"));
		container.setContentpath(scontent.getServletContext().getContextPath());
		String spath=container.getSerializationPath();		
		File f = new File(spath);
		if (!f.exists()) {
			log.info(spath+" >>Serialized not exists ...  execute make folder >>");
			f.mkdir();
			log.info("folder create successful...");
		}else{
			log.info("folder is exists execute clear...");
			File[] datalist=f.listFiles();
			for (File fdata : datalist) {
				if (fdata.getName().endsWith(".data")) {					
					log.info("del file:"+fdata.getName()+" 		>>  result:"+fdata.delete());
				}
			}
			
		}
		log.info("look for EasyTag resources folder ...");
		File easytag_res = new File(scontent.getServletContext().getRealPath("").concat("\\jeasytag_resource"));
		if (easytag_res.exists()) {
			log.info("EasyTag resources folder is exists ");
		}else{
			log.info("EasyTag resources folder not exists execute decompress ...");
			try {
				String res_path=scontent.getServletContext().getRealPath("");
				DeCompressUtil.deCompress(res_path+"\\WEB-INF\\lib\\jeasytag_resource.jar", res_path);
			} catch (Exception e) {				
				e.printStackTrace();
			}
		}
	}

	
	

	
}
